<?php
use module\home\model\User;
use module\home\model\Share;

/** 
 * load required classes
 */
require_once MODEL_DIR . '/User.php';
require_once MODEL_DIR . '/Share.php';

/**
 * define some constants
 */
define('USER_LOGIN_ID', 'USER_LOGIN_ID');
define('URL_PROFILE', '/user/profile');
define('URL_UPDATE', '/user/update');
define('URL_LOGIN', '/user/login');
define('URL_ADDFRIEND', '/user/addfriend/');
define('URL_SHARE', '/share/');

/*
 * Share Action Controller
 * 
 * extends from Action controller
 * handles share posting and listing of activities
 *
 * @package yasns
 * @subpackage controlle
 */
class ShareActionController extends ActionController
{
	/**
	 * default share action
	 * implementations are provided in the view script 
	 */
	public function executeIndex()
	{			
		$t = @$_GET['t'];
		
	
		$recentActivities = DataHelper::getAllRecentActivities(null, $t);
		$this->recentActivities = $recentActivities;
	}
	
	/**
	 * post share to database and redirect
	 */
	public function executePost()
	{
		// initialize view
      $this->errorMessage = '';
		$this->notifyMessage = '';
		$errors = array();
		
		// if user not is logged in, redirect to login
		if (!Auth::hasIdentity()) {
			Common::flashMessage("You need to login to view share.");
			Common::redirect(URL_LOGIN);
		}
		
		// if user is logged in
		// get user account id
		$user = Auth::getIdentity();
		$account_id = $user->account_id;
		
		
		// on form submisison
		if (isset($_POST, $_POST['submitShare']) && ($_POST['submitShare'] == 'Post')) {					
			// initialize form vars
			$share_content = trim($_POST['share']);
			$share_type = $_POST['type'];
		
			// validation
			if (Validator::isEmpty($share_content) || (Validator::isEmpty($share_type)))	{
				$errors[] = 'Share and Share Type cannot be empty.';
			}
			
			/*
			 * checking for any errors
			 * if not, we will continue with form processing
			 */
			if (empty($errors)) {

				// set the default timezone to use
				$timezone = date_default_timezone_set('GMT');
				$format = 'Y-m-d H:i:s';
				$share_datetime = date($format);

				// post vars to executeShare_Post();
				$share = new Share();
				$share->account_id = $account_id;
				$share->share_type = $share_type;
				$share->share_text = $share_content;
				$share->share_datetime = $share_datetime;
				$share->share_status = 'ACTIVE';
				$share->insert();
			
				//$this->notifyMessage = 'You have successful posted a share!!';
				Common::flashMessage('You have successful posted a share!');
				Common::redirect(URL_SHARE);
			} else {
				// if errors, save to flash message and redirect to display message
				Common::flashMessage(current($errors));
				Common::redirect(URL_SHARE);
			}
		}		
	}
}
?>